package com.resttest.resource;

import java.util.List;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriInfo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.wink.json4j.JSONException;
import org.apache.wink.server.utils.LinkBuilders;



//http://localhost:8080/rest_test/rest/tasks
@Path("tasks")
public  class TasksResource{


	private String resname;


	public String getResname() {
		return resname;
	}


	public void setResname(String resname) {
		this.resname = resname;
	}

	//http://localhost:8080/rest_test/rest/tasks/getmsg
	@Path("getmsg")
	@GET  
	public String getMessage() {  
		return "HelloWorld";  
	}  


	//http://localhost:8080/rest_test/rest/tasks
	@GET
	@Produces({MediaType.APPLICATION_JSON})
	public String getTasks(){

		Object res=new Object();
		List<Object> ol=new Vector<Object>();
		ol.add(res);
		
		JSONArray jsonObject = JSONArray.fromObject(ol);
		
		return jsonObject.toString();

	}


	//http://localhost:8080/rest_test/rest/tasks/tasks?tasks=2
	@Path("{tasks}")
	@GET
	@Produces({MediaType.APPLICATION_JSON})
	public String getTask(@Context LinkBuilders link,@Context UriInfo uri,@PathParam("tasks") String taskId) throws JSONException
	{
		Object res=new Object();
		JSONObject jo=JSONObject.fromObject(res);
		return jo.toString();
	}


	@POST
    @Path("/transactions/{id}")
    @Produces("application/json")
    public String saveTransactions(@PathParam("id") String id,@FormParam("name") String name) throws Exception {  
		JSONObject jo=JSONObject.fromObject(id);
		return jo.toString();
    }  


	@GET
    @Path("/transactions/{id}")
    @Produces("application/json")
    public String getTransactions(@Context HttpServletRequest request,@PathParam("id") String id,@FormParam("name") String name) throws Exception {  
		
		
		
		
		JSONObject jo=JSONObject.fromObject("{id:"+id+",name:"+request.getAttribute("test")+"}");
		return jo.toString();
    }  

}